package entities;

/**
 * Represents loan and loan status as taken out by a Student
 * 
 * @author gke703
 * @author sff803
 */
public class Loan {

	/** the unique id for the loan - given internally */
	private int id;

	/** student who is receiving the loan */
	private Student studentLoanedTo;

	/** amount of money asked for by the applicant */
	private double amtRequested;

	/** amount of money actually given by the loan */
	private double amtGiven;

	/** total amount of money given to the student */
	private double totalGiven;
	/**
	 * designated whether the loan is a newly applied(ie. hasn't been reviewed)
	 * for loan or not
	 */
	private boolean newLoan;

	/** designated whether or not the loan has been approved */
	private boolean approved;

	/** designated interest rate modifier */
	private double interest;

	/** the cosigner of the loan */
	private CoSigner cosigner;

	/**
	 * Constructor, initializes attributes
	 * 
	 * @param myStudent
	 *            , Student who is recieving the loan
	 */
	public Loan(Student myStudent) {
		studentLoanedTo = myStudent;
		amtRequested = 0;
		amtGiven = 0;
		setInterest(0);
		newLoan = true;
		approved = false;
	}

	/**
	 * accessor for the loan id
	 * 
	 * @return int, loan id
	 */
	public int getLoanId() {
		return id;
	}

	/**
	 * accessor for amount requested by applicant
	 * 
	 * @return double, amount Requested
	 */
	public double getAmountRequested() {
		return amtRequested;
	}

	/**
	 * mutator for amount requested by applicant
	 * 
	 * @param myAmt
	 *            , double, new amount requested
	 * @throws RuntimeException
	 *             , if myAmt is negative
	 */
	public void setAmountRequested(double myAmt) {
		if (myAmt < 0) {
			throw new RuntimeException("amount must be a positive number");
		}

		amtRequested = myAmt;
	}

	/**
	 * accessor for amount granted by loan
	 * 
	 * @return double, amount granted
	 */
	public double getAmountGiven() {
		return amtGiven;
	}

	/**
	 * mutator for amount granted by loan
	 * 
	 * @param myAmt
	 *            , double, new amount granted
	 * @throws RuntimeException
	 *             , if myIncome is negative
	 */
	public void setAmountGiven(double myAmt) {
		if (myAmt < 0) {
			throw new RuntimeException("amount must be a positive number");
		}

		amtGiven = myAmt;
		setTotalGiven(getTotalGiven() + myAmt);
	}

	/**
	 * accesses the student who the loan pertains to
	 * 
	 * @return Student, student who the loan pertains to
	 */
	public Student getStudent() {
		return studentLoanedTo;
	}

	/**
	 * check whether loan is new or not
	 * 
	 * @return bool, true if new, false otherwise
	 */
	public boolean isNew() {
		return newLoan;
	}

	/**
	 * change whether the loan is new or not
	 * 
	 * @param isNew
	 *            , boolean, true if loan is new false otherwise
	 */
	public void setIsNew(boolean isNew) {
		newLoan = isNew;
	}

	/**
	 * check whether the loan has been approved or not
	 * 
	 * @return approved, boolean, true if loan has been approved, false
	 *         otherwise
	 */
	public boolean isApproved() {
		return approved;
	}

	/**
	 * set whether the loan is approved or not
	 * 
	 * @param approve
	 *            , boolean, true if approved, false otherwise
	 */
	public void setApproval(boolean approve) {
		approved = approve;
	}

	/**
	 * mutator for cosigner
	 * 
	 * @param cosigner
	 *            the cosigner to set
	 */
	public void setCosigner(CoSigner cosigner) {
		this.cosigner = cosigner;
	}

	/**
	 * accessor for cosigner
	 * 
	 * @return the cosigner
	 */
	public CoSigner getCosigner() {
		return cosigner;
	}

	/**
	 * regression tests
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		Student myStudent = new Student(0, "");

		Loan myloan = new Loan(myStudent);

		if (myloan.getStudent() != myStudent) {
			System.out.println("problem initializing loan");
		}

		if (myloan.isNew() != true || myloan.isApproved() != false) {
			System.out.println("problem initializing loan");
		}

		myloan.setApproval(true);
		myloan.setIsNew(false);

		if (myloan.isApproved() != true) {
			System.out.println("problem approving loan");
		}

		if (myloan.isNew() != false) {
			System.out.println("problem marking loan as not new");
		}

		if (myloan.getAmountGiven() != 0 || myloan.getAmountRequested() != 0) {
			System.out.println("problem initializing loan");
		}

		try {
			myloan.setAmountRequested(-1000.00);
		} catch (RuntimeException e) {
			// System.out.println(e);
		}

		CoSigner c = new CoSigner(-1);
		myloan.setCosigner(c);
		if (myloan.getCosigner() != c) {
			System.out.println("problem retrieving the cosigner");
		}

	}

	private void setTotalGiven(double totalGiven) {
		this.totalGiven = totalGiven;
	}

	public double getTotalGiven() {
		return totalGiven;
	}

	private void setInterest(double interest) {
		this.interest = interest;
	}

	public double getInterest() {
		return interest;
	}

}
